package com.servlet;

import com.beans.AdminInfo;
import com.beans.CateInfo;
import com.beans.GoodsInfo;
import com.beans.PageInfo;
import com.dao.CateDao;
import com.dao.GoodsDao;
import com.util.PageUtil;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@WebServlet(name = "GoodsServlet.do", value = "/GoodsServlet.do")@MultipartConfig
public class GoodsServlet extends HttpServlet {
    private GoodsDao goodsDao = new GoodsDao();
    private CateDao cateDao = new CateDao();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String flag = request.getParameter("flag");

        if ("add".equals(flag)){
            add(request,response);
        }else if ("showPicture".equals(flag)){
            showPicture(request,response);
        }else if ("manage".equals(flag)){
            manage(request,response);
        }else if ("deleteGoods".equals(flag)){
            deleteGoods(request,response);
        }else if ("update".equals(flag)){
            update(request,response);
        }
    }

    //更新商品
    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String goodsName = request.getParameter("goodsName");
        float price = Float.parseFloat(request.getParameter("price"));
        String unit = request.getParameter("unit");
        String producter = request.getParameter("producter");
        String des = request.getParameter("des");
        int bigCateId = Integer.parseInt(request.getParameter("bigCateId"));
        int smallCateId = Integer.parseInt(request.getParameter("smallCateId"));
        int id = Integer.parseInt(request.getParameter("id"));

        Part part = request.getPart("picture");
        InputStream in = part.getInputStream();
        byte [] pictureData = new byte[in.available()];
        in.read(pictureData);

        GoodsInfo goods = new GoodsInfo();
        goods.setGoodsName(goodsName);
        goods.setPrice(price);
        goods.setUnit(unit);
        goods.setProducter(producter);
        goods.setDes(des);
        goods.setBigCateId(bigCateId);
        goods.setSmallCateId(smallCateId);
        goods.setPictureData(pictureData);
        goods.setId(id);

        int goodsId = goodsDao.updateGoods(goods);

        request.setAttribute("msg","商品修改成功");
        //回传id让前台回显图片
        request.setAttribute("goodsId",goodsId);
        request.getRequestDispatcher("/goods/goods_update.jsp").forward(request,response);
    }

    //删除商品
    private void deleteGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        goodsDao.deleteGoods(id);
        request.setAttribute("msg","<script>alert('删除成功')</script>");
        this.manage(request,response);
    }


    //商品维护列表,分页
    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bigCateId=request.getParameter("bigCateId");
        String smallCateId=request.getParameter("smallCateId");
        String goodsName=request.getParameter("goodsName");
        int pageSize = 5;
        int rowCount = goodsDao.getGoodsCount(bigCateId,smallCateId,goodsName);
        int pageIndex = 1;
        String pageIndexstr = request.getParameter("pageIndex");
        if(pageIndexstr !=null&&pageIndexstr!="") {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
            if(pageIndex>((rowCount+pageSize-1)/pageSize)) {
                pageIndex=((rowCount+pageSize-1)/pageSize);
            }
        }

        PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);

        List<GoodsInfo> goodsList= goodsDao.getAllGoods(bigCateId,smallCateId,goodsName,page);

        List<CateInfo> cateInfoList = cateDao.getAllCate();
        request.setAttribute("cateInfoList",cateInfoList);
        request.setAttribute("goodsList", goodsList);
        request.setAttribute("page", page);
        request.setAttribute("bigCateId",bigCateId);
        request.setAttribute("smallCateId",smallCateId);
        request.setAttribute("goodsName",goodsName);

        request.getRequestDispatcher("/goods/goods_manage.jsp").forward(request, response);
    }

    //显示某个商品的图片
    private void showPicture(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int goodsId = Integer.parseInt(request.getParameter("goodsId"));

        GoodsInfo goods = goodsDao.getGoodsById(goodsId);

        response.setContentType("image/jpg");

        ServletOutputStream outStream = response.getOutputStream();
        outStream.write(goods.getPictureData());
        outStream.flush();
    }

    //添加商品并上传图片
    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String goodsName = request.getParameter("goodsName");
        float price = Float.parseFloat(request.getParameter("price"));
        String unit = request.getParameter("unit");
        String producter = request.getParameter("producter");
        String des = request.getParameter("des");
        int bigCateId = Integer.parseInt(request.getParameter("bigCateId"));
        int smallCateId = Integer.parseInt(request.getParameter("smallCateId"));

        Part part = request.getPart("picture");
        InputStream in = part.getInputStream();
        byte [] pictureData = new byte[in.available()];
        in.read(pictureData);

        GoodsInfo goods = new GoodsInfo();
        goods.setGoodsName(goodsName);
        goods.setPrice(price);
        goods.setUnit(unit);
        goods.setProducter(producter);
        goods.setDes(des);
        goods.setBigCateId(bigCateId);
        goods.setSmallCateId(smallCateId);
        goods.setPictureData(pictureData);

        int goodsId = goodsDao.addGoods(goods);

        request.setAttribute("msg","商品添加成功");
        //回传id让前台回显图片
        request.setAttribute("goodsId",goodsId);
        request.getRequestDispatcher("/goods/goods_add.jsp").forward(request,response);
    }
}
